package com.sigmatrix.h5.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sigmatrix.h5.dao.DisTerminalShopDetailDao;
import com.sigmatrix.h5.entity.DisDealerAcctRegAudit;
import com.sigmatrix.h5.entity.DisTerminalShop;
import com.sigmatrix.h5.entity.DisTerminalShopDetail;
import com.sigmatrix.h5.service.DisTerminalShopDetailService;
import com.sigmatrix.h5.service.DisTerminalShopService;
import com.sigmatrix.h5.utils.Assert;
import com.sigmatrix.h5.utils.Constants;
import com.sigmatrix.sc.jdbc.common.service.ServiceImpl;

/**
 * 
 *@ClassName: DisTerminalShopDetailServiceImpl
 *@Description: 
 *@author MH
 *@date 2017年12月5日 下午5:09:22
 */
@Service
public class DisTerminalShopDetailServiceImpl extends ServiceImpl<DisTerminalShopDetail,Integer> implements DisTerminalShopDetailService{
	@Autowired
	private DisTerminalShopDetailDao disTerminalShopDetailDao;
	@Autowired
	private DisTerminalShopService disTerminalShopService;
	@Override
	public List<DisTerminalShopDetail> findByTerminalShopId(Integer terminalShopId) {
		DisTerminalShopDetail shop = new DisTerminalShopDetail();
		shop.setTerminalShopId(terminalShopId);
		return select(shop);
	}

	@Override
	@Deprecated
	public int selectCountByTerminalNameAndDealerId(String name, Integer dealerId) {
		return disTerminalShopDetailDao.selectCountByTerminalNameAndDealerId(name, dealerId);
	}

	@Override
	public int selectCountByOpenId(String openId, Integer dealerId) {
		DisTerminalShopDetail shop = new DisTerminalShopDetail();
		shop.setOpenId(openId);
		shop.setStatus(Constants.STATUS_ENABLE);
		return disTerminalShopDetailDao.selectCount(shop);
	}

	@Override
	public int selectCountByDealerIdAndShopId(Integer dealerId, Integer shopId) {
		DisTerminalShopDetail d = new DisTerminalShopDetail();
		d.setDealerId(dealerId);
		d.setTerminalShopId(shopId);
		return disTerminalShopDetailDao.selectCount(d);
	}

	@Override
	public int copy(String shopCode,DisDealerAcctRegAudit audit) {
		DisTerminalShop shop = disTerminalShopService.findByCode(shopCode);
		Assert.notNull(shop, "终端门店编码错误，不能保存明细信息");
		Assert.notNull(audit, "审核信息为空，不能保存明细信息");
		return create(shop, audit.getDealerId(), audit.getOpenId());
	}

	@Override
	public int create(DisTerminalShop shop, Integer dealerId,String openId) {
		DisTerminalShopDetail detail = new DisTerminalShopDetail();
		detail.setAddress(shop.getAddress());
		detail.setCity(shop.getCity());
		detail.setCitySubOrdinate(shop.getCitySubOrdinate());
		detail.setContact(shop.getContact());
		detail.setCreateTime(new Date());
		detail.setCreatorId(shop.getCreatorId());
		detail.setDealerId(dealerId);
		detail.setOpenId(openId);
		detail.setSmallOpenId(shop.getSmallOpenId());
		detail.setMobilePhone(shop.getMobilePhone());
		detail.setProvince(shop.getProvince());
		detail.setRemarks(shop.getRemarks());
		detail.setStatus(Constants.STATUS_ENABLE);
		detail.setTerminalShopId(shop.getId());
		return insertSelective(detail);
	}

	@Override
	public List<DisTerminalShopDetail> findByOpenId(String openId) {
		DisTerminalShopDetail detail = new DisTerminalShopDetail();
		detail.setOpenId(openId);
		Map<String,String> order = new HashMap<String,String>();
		order.put("createTime", "desc");
		return select(detail,order);
	}

}
